import Url from 'url-parse';

/**
 * 查询 `url` 上的参数
 */
const getQuery = (url = '') => new Url(url, true).query;

/**
 * 获取 `url` 上的 `qrcode` 参数
 */
export const getQRCodeUrl = () => {
  return new Promise<string>((resolve) => {
    const tips = '未获取到页面地址';
    try {
      chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
        const { url } = tabs[0];
        resolve(getQuery(url).qrcode || url || tips);
      });
    } catch (err) {
      resolve(getQuery(location.href).qrcode || tips);
    }
  });
};
